package online.sixfish.codegenerator.mybatis;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import org.apache.ibatis.type.JdbcType;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class MybatisCodeGenerator {


    static String DB_NAME="online_hospital";
    static String DB_USER_NAME="root";
    static String DB_PASSWORD="password";
    static String XML_PATH="module/src/main/resources/mapper";
//    static String CODE_PATH="module.src.main.java";
    static String CODE_PATH="src.main.java";
    static String PACKAGE="online.sixfish.codegenerator";
    static String AUTHOR="liuyu";

    /**
     *需要生成的表名 以,分割。如a,b,c。
     */
    static String INCLUDE="category";

    public static void main(String[] args) {
        String url = "jdbc:mysql://192.168.0.2:3306/"+DB_NAME+"?allowPublicKeyRetrieval=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&useAffectedRows=true";
        DataSourceConfig.Builder dataSource = new DataSourceConfig
                .Builder(url, DB_USER_NAME, DB_PASSWORD)
                .keyWordsHandler(new MySqlKeyWordsHandler());
        String xmlPath=Paths.get(System.getProperty("user.dir"),XML_PATH.split("/")).toString();
        Map<OutputFile, String> map = new HashMap<>();
        map.put(OutputFile.controller,null);
        map.put(OutputFile.service,null);
        map.put(OutputFile.serviceImpl,null);
        map.put(OutputFile.xml,xmlPath);

        FastAutoGenerator.create(dataSource)
                .dataSourceConfig(
                        builder -> builder.typeConvertHandler(((globalConfig, typeRegistry, metaInfo) -> {

                            if (JdbcType.BIGINT == metaInfo.getJdbcType()) {
                                return DbColumnType.LONG;
                            }
                            if (JdbcType.TINYINT == metaInfo.getJdbcType()) {
                                return DbColumnType.INTEGER;
                            }
                            if (JdbcType.INTEGER == metaInfo.getJdbcType()) {
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        }))
                )
                .globalConfig(builder -> builder
                        .author(AUTHOR)
                        .outputDir(Paths.get(System.getProperty("user.dir"),
                                CODE_PATH.split("\\.")).toString())
                        .commentDate("yyyy-MM-dd")
                        .disableOpenDir()

                )
                .packageConfig(builder -> builder
                        .parent(PACKAGE)
                        .entity("entity")
                        .mapper("mapper")
                        .pathInfo(map)

                )

                .strategyConfig(builder -> builder
//                        .addInclude(INCLUDE)
                        .serviceBuilder().serviceTemplate("/template/service.java")
                        .serviceBuilder().disableServiceImpl()
                        .controllerBuilder().disable()
                        .entityBuilder().javaTemplate("/svtemplate/entity.java")
                        .mapperBuilder().mapperTemplate("/svtemplate/mapper.java")
                        .mapperXmlTemplate("/template/mapper.xml")
                        .enableFileOverride()


                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();


    }

}
